package com.whfc.ms.api.emp.service.impl;

import com.whfc.common.result.ListData;
import com.whfc.emp.constant.EmpConstant;
import com.whfc.emp.dto.*;
import com.whfc.emp.service.AppEmpConfigService;
import com.whfc.emp.service.AppEmpIndexService;
import com.whfc.fvs.dto.FvsDeviceDTO;
import com.whfc.fvs.service.FvsDeviceService;
import com.whfc.ms.api.emp.service.MsAppEmpIndexService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClasssName MsAppEmpIndexServiceImpl
 * @Description 首页相关的接口服务
 * @Author hw
 * @Date 2020/12/30 15:37
 * @Version 1.0
 */
@Service
public class MsAppEmpIndexServiceImpl implements MsAppEmpIndexService {

    @DubboReference(interfaceClass = AppEmpIndexService.class, version = "1.0.0")
    private AppEmpIndexService appEmpIndexService;

    @DubboReference(interfaceClass = FvsDeviceService.class, version = "1.0.0")
    private FvsDeviceService fvsDeviceService;

    @DubboReference(interfaceClass = AppEmpConfigService.class, version = "1.0.0")
    private AppEmpConfigService appEmpConfigService;

    @Override
    public ListData<AppWorkTypeDTO> getWorkTypeList(Integer deptId) {
        List<AppWorkTypeDTO> list = appEmpIndexService.getWorkTypeList(deptId);
        return new ListData<>(list);
    }

    @Override
    public AppEmpDetailDTO getEmpDetail(Integer empId) {
        return appEmpIndexService.getEmpDetail(empId);
    }

    @Override
    public ListData<AppEmpDTO> getMapEmpList(Integer deptId, Integer groupId, String empName) {
        List<AppEmpDTO> list = appEmpIndexService.getMapEmpList(deptId, groupId, empName);
        return new ListData<>(list);
    }

    @Override
    public ListData<MapEmpPolyDTO> getMapEmpPoly(Integer deptId, Integer groupId, String empName, Integer fenceId) {
        List<MapEmpPolyDTO> list = appEmpIndexService.getMapEmpPoly(deptId, groupId, empName, fenceId);
        return new ListData<>(list);
    }

    @Override
    public ListData<MapEmpGroupDTO> getMapEmpFvs(Integer deptId, Integer groupId, String empName, Integer fvsDeviceId) {
        List<MapEmpGroupDTO> list = appEmpIndexService.getMapEmpFvs(deptId, groupId, empName, fvsDeviceId);
        List<MapEmpGroupDTO> groupList = new ArrayList<>();
        if (!list.isEmpty()) {
            //获取监控位置信息
            for (MapEmpGroupDTO mapEmpGroupDTO : list) {
                //获取班组的监控信息
                Integer deviceId = mapEmpGroupDTO.getFvsDeviceId();
                //获取监控位置信息
                FvsDeviceDTO fvsDevice = fvsDeviceService.detail(deviceId);
                if (fvsDevice == null) {
                    continue;
                }
                Double lat = fvsDevice.getLat();
                Double lng = fvsDevice.getLng();
                if (lat == null || lng == null) {
                    continue;
                }
                mapEmpGroupDTO.setLat(fvsDevice.getLat());
                mapEmpGroupDTO.setLng(fvsDevice.getLng());
                mapEmpGroupDTO.setDeviceStatus(fvsDevice.getDeviceStatus());
                mapEmpGroupDTO.setStreamMode(fvsDevice.getStreamMode());
                groupList.add(mapEmpGroupDTO);
            }
        }
        return new ListData<>(groupList);
    }

    @Override
    public boolean getIndexFvsTab(Integer deptId) {
        AppEmpConfigDTO empConfig = appEmpConfigService.getEmpConfig(deptId, EmpConstant.EMP_INDEX_FVS);
        if (empConfig != null) {
            return true;
        }
        return false;
    }
}
